package com.ssyc.gsk_teacher_appraisal.activity;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.bean.ResultInfo;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.PickerManager;
import com.ssyc.common.manager.PopUpManager;
import com.ssyc.common.utils.DateUtils;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.SPUtil;
import com.ssyc.common.utils.UIHelper;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.dialog.CustomDialogManager;
import com.ssyc.gsk_teacher_appraisal.R;
import com.ssyc.gsk_teacher_appraisal.adapter.PopLvAdapter;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 教师端测评之考试发布
 * Created by Kevin on 2018/10/31
 */
public class TkTeacherAppraisalTestReleaseActivity extends BaseActivity implements View.OnClickListener {

    //返回
    private ImageView iv_back;
    //发布按钮
    private ImageView iv_commit;
    //考试套题名称
    private TextView tv_test_name;
    //推送班级名称
    private TextView tv_class_name;
    //提交时间按钮
    private RelativeLayout rl_submit_calendar;
    //提交日期显示
    private TextView tv_submit_calendar;
    //设置考试时长按钮
    private RelativeLayout rl_set_time;
    //考试时长显示
    private TextView tv_test_time;

    //考试时长选择适配器
    private PopLvAdapter popLvAdapter;

    //班级id
    private String classnum;
    //套题id
    private String row_id;
    //套题名称
    private String testName;
    //截止日期
    private String time;
    private String date;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_tk_teacher_appraisal_test_release;
    }


    @Override
    protected void init() {
        showContent();

        //接收数据
        initIntent();

        //初始化视图
        initView();

    }

    /**
     * 接收数据
     */
    private void initIntent() {

        classnum = SPUtil.getString(this,"classnum");
        row_id = getIntent().getStringExtra("rowId");
        testName = getIntent().getStringExtra("testName");
    }

    /**
     * 初始化视图
     */
    public void initView() {

        iv_back = findView(R.id.iv_back);
        //套题名称
        tv_test_name = findView(R.id.tv_test_name);
        tv_test_name.setText(testName);

        //班级名称
        tv_class_name = findView(R.id.tv_class_name);
        tv_class_name.setText(SPUtil.getString(this,"CPclassname"));

        //显示发布时间
        tv_submit_calendar = findView(R.id.tv_submit_calendar);

        //显示考试时长
        tv_test_time = findView(R.id.tv_test_time);

        rl_submit_calendar = findView(R.id.rl_submit_calendar);
        rl_set_time = findView(R.id.rl_set_time);
        iv_commit = findView(R.id.iv_commit);

        rl_submit_calendar.setOnClickListener(this);
        rl_set_time.setOnClickListener(this);
        iv_back.setOnClickListener(this);
        iv_commit.setOnClickListener(this);

        //日期
        date = DateUtils.getFetureDate(7);
        //中文日期7天后
        time = DateUtils.getFetureTime(7);
        //设置默认日期
        tv_submit_calendar.setText(time+" 24点整");

    }


    /**
     * 点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        } else if (id == R.id.rl_submit_calendar) {
            //发布截止日期
            showDatePick();
        } else if (id == R.id.rl_set_time) {
            //设置考试时长
            setTime();
        } else if (id == R.id.iv_commit) {

            //判断截止时间是否合法
            if(!DateUtils.isDateOneWeek(date)){
                UIHelper.showToast(this,"截止日期需为7日内");
                return;
            }else{
                //防止连点
                if (UiUtils.isNotFastClick()) {
                    //提交发布考试
                    doUploading();
                }
            }
        }
    }

    /**
     * 发布数据
     */
    private void doUploading() {
        CustomDialogManager.show(this,"发布中...");

        Map<String, String> map = new HashMap<>();
        map.put("type", "2");
        map.put("ttel", AccountUtils.getAccount(this));
        map.put("classnum", classnum);
        map.put("row_id", row_id);
        map.put("lastdate", date);
        map.put("examtime", tv_test_time.getText().toString().substring(0, 2));

        Log.i("test","classnum："+classnum+"row_id:"+row_id+"examtime:"+tv_test_time.getText().toString().substring(0, 2));

        OkHttpUtils.post().url(HttpAdress.CPMain).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                UiUtils.Toast("服务器异常,上传失败", false);
                CustomDialogManager.dissmiss();
            }

            @Override
            public void onResponse(String response, int id) {
                CustomDialogManager.dissmiss();
                Log.i("test","发布套题："+response);
                ResultInfo info = null;
                try {
                    info = GsonUtil.jsonToBean(response, ResultInfo.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                if ("200".equals(info.state)) {
                    //发送消息
                    BusInfo busInfo = new BusInfo();
                    busInfo.msg = TkTeacherAppraisalMainActivity.CPUPDATEPAGEDATA;
                    EventBus.getDefault().post(busInfo);

                    UiUtils.Toast("上传成功", false);
                    finish();
                }else{
                    UiUtils.Toast("上传失败，错误代码"+info.state, false);
                }
            }
        });
    }

    /**
     * 设置时间
     */
    private void setTime() {
        final List<String> contents = new ArrayList<>();
        contents.add("10分钟");
        contents.add("15分钟");
        contents.add("20分钟");
        contents.add("25分钟");
        contents.add("30分钟");
        contents.add("35分钟");
        contents.add("40分钟");
        //弹框
        PopUpManager.showPop(this, R.layout.teacher_pop_item_common, 0.3f, new PopUpManager.onGetViewListener() {
            @Override
            public void getChildView(View view, int layoutResId) {
                //标题
                TextView tvTitle = view.findViewById(R.id.tv_title);
                //列表
                ListView lv = view.findViewById(R.id.lv);
                //取消
                TextView tvCancel = view.findViewById(R.id.tv_cancel);

                tvTitle.setText("设置时长");

                //设置列表数据
                popLvAdapter = new PopLvAdapter(TkTeacherAppraisalTestReleaseActivity.this, contents,
                        R.layout.teacher_lv_item_pop_common);
                lv.setAdapter(popLvAdapter);

                //条目点击事件
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                        tv_test_time.setText(contents.get(pos));
                        PopUpManager.dismiss();
                    }
                });

                //取消的点击事件
                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUpManager.dismiss();
                    }
                });
            }
        });
    }

    /**
     * 选择日期的方法
     */
    private void showDatePick() {
        PickerManager.showPickerByYeaerAndMonthAndDay(this, new PickerManager.onSelectedListener() {
            @Override
            public void onTimeSelect(Date d, View v) {
                //选中后的回调
                date = DateUtils.getTime(d, "yyyy-MM-dd");

                tv_submit_calendar.setText(DateUtils.getTime(d, "yyyy年MM月dd日")+" 24点整");
            }
        });
    }
}
